// pages/mine/task/index.js
const app = getApp();
const time = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    role: app.register_info.register_role,
    //主要信息
    balance: [],
    code: '',
    extract_time: 0,
    receive_time: 0,

    //参数
    withdraw: false,
    rpx: '',
    page: 1,

    //弹框
    flag_rule: true,
    flag_canvas: true,
    flag_share: true,
    flag_ruleTwo: true,

    flag_check: true,

    flag_concern:false,

    //多次点击

    //切换
    currentData: 0,
  },
  //复制微信、公众号
  click_copy_wechat(e){
    console.log(e)
    let wechat = e.currentTarget.dataset.wechat;
    wx.setClipboardData({
      data: wechat,
      success: function (e) {}
    })
  },
  //去关注
  go_concern() {
    this.setData({
      flag_concern: true,
    })
  },
  //去领取
  go_receive() {
    this.receive();
  },
  // 招聘
  go_recruit() {
    wx.reLaunch({
      url: '../../recruit/index/index?flag_falls=true'
    })
  },
  getSign_Re() {
    wx.request({
      url: app.globalData.url + 'api/register/sign/in/sign',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: (e) => {
        console.log(e)
        let current_num = e.data.data.current_num;
        this.setData({
          current_num: current_num,
        })
      },
    })
  },
  click_gives() {
    this.getGives();
  },
  //登录好礼领取
  getGives() {
    wx.request({
      url: app.globalData.url + 'api/login/gives/get',
      data: {
        token: app.globalData.token,
        type: 1,
      },
      method: 'POST',
      success: (e) => {
        console.log(e.data.message);
        wx.showToast({
          title: '领取成功',
          icon: 'none'
        })
        this.getTask();
      },
    })
  },
  //每日任务
  getTask() {
    wx.request({
      url: app.globalData.url + 'api/task/everyday/mission',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: (e) => {
        console.log(e)
        let signInfo = e.data.data;
        this.setData({
          signInfo: signInfo
        })
      },
    })
  },
  // 去规则说明页
  go_rule() {
    wx.navigateTo({
      url: 'signRule/signRule'
    })
  },
  // 点击签到
  click_sign() {
    this.sign();
  },
  // 领取请求
  receive() {
    wx.request({
      url: app.globalData.url + 'api/task/send/resume/draw',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: (e) => {
        // console.log(e)
        this.getSign();
        this.getTask();
      }
    })
  },
  // 签到请求
  sign() {
    wx.request({
      url: app.globalData.url + 'api/register/sign/in/sign',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: (e) => {
        // console.log(e)
        this.getSign();
        let current_num = e.data.data.current_num;
        this.setData({
          flag_check: false,
          current_num: current_num,
        })

      }
    })
  },
  //去签到
  go_sign() {
    wx.navigateTo({
      url: '../task/index?show=show_check'
    })
  },
  //获取签到
  getSign() {
    wx.request({
      url: app.globalData.url + 'api/register/sign/in/list',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: (e) => {
        // console.log(e)
        var sign = e.data.data,
          one_flag = false,
          two_flag = false,
          thr_flag = false,
          fou_flag = false,
          fiv_flag = false,
          six_flag = false,
          sev_flag = false;
        if (sign.is_sign == 1) {
          var num = 0;
        } else {
          var num = parseInt(sign.sitcom_sign_number);
        }
        console.log(num)
        switch (num) {
          case 1:
            var one_flag = true;
            break;
          case 2:
            var one_flag = true;
            var two_flag = true;
            break;
          case 3:
            var one_flag = true;
            var two_flag = true;
            var thr_flag = true;
            break;
          case 4:
            var one_flag = true;
            var two_flag = true;
            var thr_flag = true;
            var fou_flag = true;
            break;
          case 5:
            var one_flag = true;
            var two_flag = true;
            var thr_flag = true;
            var fou_flag = true;
            var fiv_flag = true;
            break;
          case 6:
            var one_flag = true;
            var two_flag = true;
            var thr_flag = true;
            var fou_flag = true;
            var fiv_flag = true;
            var six_flag = true;
            break;
          case 7:
            var one_flag = true;
            var two_flag = true;
            var thr_flag = true;
            var fou_flag = true;
            var fiv_flag = true;
            var six_flag = true;
            var sev_flag = true;
          default:
            console.log("none")
        }
        this.setData({
          sign: sign,
          is_today_sign: sign.is_today_sign,
          one_flag: one_flag,
          two_flag: two_flag,
          thr_flag: thr_flag,
          fou_flag: fou_flag,
          fiv_flag: fiv_flag,
          six_flag: six_flag,
          sev_flag: sev_flag,
        })
      }
    })
  },
  // 获取规则
  getRule() {
    wx.request({
      url: app.globalData.url + 'api/broker/lift/rule',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: (e) => {
        let welfare = e.data.data.welfare,
          rule = e.data.data.rule,
          upgrade = e.data.data;
        this.setData({
          upgrade: upgrade,
        })
      },

    })
  },
  //去升级
  go_upgrade() {
    wx.navigateTo({
      url: '../upgrade/upgrade',
    })
  },
  //查看公司
  go_check() {
    wx.navigateTo({
      url: '../anchorWelfare/check/check',
    })
  },
  //点击选项
  click_all(e) {
    // console.log(e)
    let num = e.currentTarget.dataset.num;
    if (num == 1) {
      this.setData({
        no_resume: 0,
        page: 1,
      })
      this.get_invite(0, 1);
    } else if (num == 2) {
      this.setData({
        no_resume: 1,
        page: 1,
      })
      this.get_invite(1, 1);
    }
  },


  //滚动加载
  loadDetail: function(e) {
    let that = this;
    let page = this.data.page;
    page = page + 1;
    this.setData({
      page: page
    })
    this.List_invite();
  },

  // 小播推荐
  List_invite: function(e) {
    let that = this;
    let page = this.data.page,
      no_resume = this.data.no_resume;
    that.get_invite(no_resume, page);
  },

  //邀请记录
  get_invite(no_resume, page) {
    wx.request({
      url: app.globalData.url + 'api/invitation/my/invite',
      data: {
        token: app.globalData.token,
        no_resume: no_resume,
        page: page
      },
      method: 'POST',
      success: (e) => {
        // console.log(e)
        if (e.data.data.current_page > e.data.data.total_page) {
          console.log("到底了");
          wx.showToast({
            title: '到底啦~',
            icon: 'none',
          })
        } else {
          if (this.data.page == 1) {
            let bills = e.data.data.bills;
            bills.forEach(function(value, index) {
              value.create_time = time.formatTimeTwo(value.create_time, 'Y-M-D h:m');
            })
            this.setData({
              bills: bills,
            })
          } else {
            // console.log("不不不不不");
            let bills = that.data.bills;
            let _bills = e.data.data.bills;
            _bills.forEach(function(value, index) {
              value.create_time = time.formatTimeTwo(value.create_time, 'Y-M-D h:m');
            })
            bills.push(..._bills);
            //   console.log(CollectList);
            this.setData({
              bills: bills,
            })
          }
          wx.hideLoading();
        }
      },
    })
  },

  //隐藏弹框
  modal_hide() {
    this.setData({
      flag_share: true,
      flag_check: true,
      flag_concern: false,
    })
  },
  //加载个人信息
  getInfo() {
    wx.request({
      url: app.globalData.url + 'api/common/register/info',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: (e) => {
        // console.log(e)
        let info = e.data.data,
          realMoney = parseInt(info.bb) ? parseInt(info.bb) / this.data.balance.bb_rate : 0;
        this.setData({
          info: info,
          realMoney: realMoney,
        })
      },
    })
  },
  //分享链接
  click_url() {
    let that = this;
    let balance = that.data.balance;
    let share_url = balance.share_url;
    let code = balance.code;
    wx.showModal({
      content: '已复制，快去分享给好友吧！',
      showCancel: false,
      confirmText: '好的',
      confirmColor: '#d0a76c',
      success: function(e) {
        // console.log(e)
        if (e.confirm) {
          wx.setClipboardData({
            data: share_url + code,
          })
          that.setData({
            flag_share: true,
          })
        }
      },
    })
  },
  //分享图片
  click_img() {
    this.setData({
      flag_share: true,
      flag_canvas: false,
    })
  },
  //查看体现详情
  go_detail() {
    let that = this;
    wx.navigateTo({
      url: '../welfareDetail/welfareDetail',
    })
  },
  //邀请
  click_invite() {
    this.balance();
    wx.navigateTo({
      url: '../invite/index'
    })

  },
  //隐藏
  click_hide() {
    this.setData({
      flag_rule: true,
      flag_ruleTwo: true,
    })
  },
  //复制
  click_copy() {
    let code = this.data.balance.code;
    wx.setClipboardData({
      data: code,
    })
  },
  //领取
  go_receive() {
    var that = this;
    var balance = this.data.balance;
    if (balance.has_resume == "2") {
      wx.showToast({
        title: '简历审核中，暂不能操作',
        icon: 'none',
      })
    } else if (balance.has_resume == "3") {
      wx.showToast({
        title: '简历审核失败，请重新上传',
        icon: 'none',
      })
    } else if (balance.has_resume == "1") {
      var receive_time = this.data.receive_time;
      receive_time = receive_time + 1;
      this.setData({
        receive_time: receive_time
      })
      this.balance();
      if (this.data.receive_time == 1) {
        wx.request({
          // url: app.globalData.url + 'api/invitation/user/receive',
          url: app.globalData.url + 'api/task/send/resume/draw',
          data: {
            token: app.globalData.token,
          },
          method: 'POST',
          success: (e) => {
            wx.showToast({
              title: '领取成功',
              icon: 'none'
            })
            // console.log(e)
            this.balance();
            this.getTask();
          },
        })
      }
    }

  },
  //去简历
  go_resume() {
    let that = this,
      balance = this.data.balance;
    if (balance.has_resume == 0) {
      wx.navigateTo({
        url: '../../resume/uploadResume/uploadResume?welfare=true',
      })
    } else if (balance.has_resume == 2) {
      wx.showToast({
        title: '简历正在审核中',
        icon: 'none',
      })
    } else if (balance.has_resume == 3) {
      wx.showModal({
        content: '审核失败，请重新发布',
        showCancel: true,
        confirmText: '重新发布',
        confirmColor: '#d0a76c',
        success: function(e) {
          if (e.confirm) {
            wx.navigateTo({
              url: '../../resume/uploadResume/uploadResume?welfare=true',
            })
          }
        },
      })
    }

  },
  //获取我的额度 等信息
  balance() {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/invitation/my/balance',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: function(e) {
        // console.log(e)
        let balance = e.data.data;
        let code = e.data.data.code;
        that.setData({
          balance: balance,
          code: code,
        })
      },
    })
  },
  now_sign() {
    let that = this;
    wx.showLoading()
    //立即签到
    // this.getSign();
    var timer = setTimeout(function() {
      var is_today_sign = that.data.is_today_sign;
      // console.log('is_today_sign+' + is_today_sign);
      if (is_today_sign == 1) {
        // console.log('checked')
      } else {
        that.sign()
      }
      wx.hideLoading();
      clearTimeout(timer)
    }, 500)

  },
  //切换 Start
  //获取当前滑块的index
  bindchange: function(e) {
    const that = this;
    that.setData({
      currentData: e.detail.current
    })
  },
  //点击切换，滑块index赋值
  checkCurrent: function(e) {
    const that = this;
    if (that.data.currentData === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentData: e.target.dataset.current
      })
    }
  },
  //切换 End

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    // console.log(options)
    switch (options.show) {
      case 'show_check':
        this.setData({
          show_check: true
        })
        wx.setNavigationBarTitle({
          title: '签到' //页面标题为路由参数
        })
        this.now_sign();
        break;
      case 'show_task':
        this.setData({
          show_task: true
        })

        break;
      default:
        console.log('none');
    }
    if (options.current) {
      this.setData({
        currentData: 1
      })
    }
    // this.get_invite();
    // this.createCanvas();
    wx.getSystemInfo({
      success: function(e) {
        const rpx = e.windowWidth / 375;
        that.setData({
          rpx: rpx,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.balance();
    this.getInfo();
    this.getRule();
    this.getSign();
    this.getTask();
    // this.getSign_Re();
    this.setData({
      role: app.register_info.register_role,
    })
    // console.log('role+'+this.data.role)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})